<?php
namespace internalapi\controllers;

use Yii;
use common\forms\LoginForm;
use heihei\rest\Controller;



/**
 * 默认控制器
 */
class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function actionError(){
        return [
            'name' => $this->getExceptionName(),
            'message' => $this->getExceptionMessage(),
            'exception' => $this->exception,
        ];
    }

    /**
     * 平台首页.
     *
     * @return string
     */
    public function actionIndex()
    {
        return "平台管理";
    }

    /**
     * 登录
     *
     * @return string
     */
    public function actionLogin()
    {
        $loginForm = new LoginForm();
        $loginForm->load(Yii::$app->request->post(), '');
        if ($loginForm->login()) {
            return $loginForm->getJwtToken();
        }
        $error = $loginForm->getFirstErrors();
        return $this->fail(reset($error));
    }

    /**
     * 登录
     *
     * @return string
     */
    public function actionWeixinLogin()
    {
        $loginForm = new WeixinLoginForm();
        $loginForm->load(Yii::$app->request->post(), '');
        if ($loginForm->login()) {
            return $loginForm->getJwtToken();
        }
        $error = $loginForm->getFirstErrors();
        return $this->fail(reset($error));
    }

    /**
     * 账号退出
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
}
